const express = require('express');
const router = express.Router();
const StudentActivity = require('../../models/SjoinActivity/SjoinActivity');

// 取消学生活动报名
router.delete('/cancelJoin/:studentId/:activityId', async (req, res) => {
  try {
    const { studentId, activityId } = req.params;

    // 查找报名记录
    const registration = await StudentActivity.findOne({
      where: {
        student_id: studentId,
        activity_id: activityId,
        status: 'registered' // 只能取消已报名但未参加的活动
      }
    });

    // 检查报名记录是否存在
    if (!registration) {
      return res.status(404).json({
        success: false,
        message: '未找到报名记录'
      });
    }

    // 删除报名记录
    await registration.destroy();

    res.json({
      success: true,
      message: '取消报名成功'
    });
  } catch (error) {
    console.error('取消报名失败:', error);
    res.status(500).json({
      success: false,
      message: '取消报名失败',
      error: error.message
    });
  }
});

module.exports = router;
